define(["app","js/main/mainView","js/list/listController", "jQuery","js/init", 'js/utils','js/initSocket'], 
	function(app, patientView,lc, $,base,Utils,initSocket) {
	
	
	var bindings = [{
		element: '.get-storage-data',
		event: 'click',
		handler: login
	}];

	function init(query){	
		
		patientView.render({			
			bindings: bindings
		});
	}

	function login() {
		
		if(!app.mainView){
			app = app.getApp();
		}
		var storedData = {
			loginname : $('#loginname').val(),
			password:$('#password').val()
		};
				
			  
		if(storedData) {

			//获取密钥
			//btnEvent.getKeys();
			//var encrypt = new jsencrypt.JSEncrypt();
			//encrypt.setPublicKey(pk.p);

			//var _p = {
				//userid : storedData.userid,
				//password:storedData.password
			//}
			//加密密码
			//var encrypted = encrypt.encrypt(JSON.stringify(_p));			

			var _param = {
				url	: base.base + base.login,
				data: storedData,
				success:function(data){

					if(data.retStatus ==='success'){
						
						//初始化socket
						initSocket.init(data.userid)

						app.setUser(data)					
						
						app.f7.closeModal();

						//重新初始化列表页面
						lc.init();

						//检查更新

						setTimeout(function(){							
						    Utils.checkUpdate(app);
						},5000);

						//alert('login success...');
						//app.mainView.loadPage('patient.html',true);
						//Router.load('patient'); 
					}else{
						app.f7.addNotification({
							message: '用户名或密码错误',
							hold   : 2000
						});
											
						
					}
				}
				,
				error:function(){
					app.f7.addNotification({
						message: '登录失败，请再次尝试，如问题依旧，请联系客服进行处理',
						hold   : 2000
					});
					
				}
			}
			$.ajax(_param)
			//alert(JSON.stringify(storedData));
			}
		else {
			alert('There is no stored data for this form yet. Try to change any field')
		}
		
		
	}

	return {
		init: init
	};
});